package com.example.myapplication.Outpatientappointment;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.MakeanappointBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import okhttp3.Response;

public class MakeanappointmentActivity extends AppCompatActivity {

    private Button but_makeanappointment;
    private EditText ed_Makeanappointment;
    private RadioGroup radioGroup1;
    private Button btn_ok;
    private String administrativeofficeID;
    private String administrativeofficeMoney;
    private TextView text_data;
    private Calendar calendar;
    private String data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_makeanappointment);
        Intent intent = getIntent();
        //门诊分类的id
        administrativeofficeID = intent.getStringExtra("AdministrativeofficeID");
        //门诊收费的价格
        administrativeofficeMoney = intent.getStringExtra("AdministrativeofficeMoney");
        initview();
        //返回
        but_Bloak();
        //预约时间
        getsendtime();
        //拿到发送的参数
        getJSON();

    }

    private void but_Bloak() {
        but_makeanappointment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MakeanappointmentActivity.this.finish();
            }
        });
    }

    private void getsendtime() {
        text_data.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onClick(View v) {
                DatePickerDialog datePickerDialog = new DatePickerDialog(MakeanappointmentActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        Date date = new Date();
                        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); // 指定时间格式
                        String currentTime = sdf.format(date); // 将Date对象格式化为字符串
                        System.out.println(currentTime); // 输出当前时间
                        data = year + "-" + month +"0" + "-" + dayOfMonth + " " +currentTime;
                    }
                }, 2023,9,1);
                datePickerDialog.show();
            }
        });
    }

    private void getJSON() {
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int checkedRadioButtonId = radioGroup1.getCheckedRadioButtonId();
                String type = "";
                //普通
                if(R.id.radio0==checkedRadioButtonId){
                    type="2";
                }else if(R.id.radio1==checkedRadioButtonId){
                    //专家
                    type="1";
                }
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("categoryId",administrativeofficeID);
                    jsonObject.put("money",administrativeofficeMoney);
                    jsonObject.put("patientName",ed_Makeanappointment.getText().toString());
                    jsonObject.put("reserveTime",data);
                    jsonObject.put("type",type);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //生成预约单
                sendmakeanappointment(jsonObject.toString());
            }
        });
    }

    private void sendmakeanappointment(String JSONString) {
        OKHttpTool.doNetRequest("prod-api/api/hospital",
                OKHttpTool.POST,
                MakeanappointBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        MakeanappointBean response = (MakeanappointBean)obj;
                        if(response.getCode()==200){
                        }
                    }
                    @Override
                    public void onFailure(String msg) {
                        Log.e("response","msg:"+msg);
                    }
        },JSONString);
    }

    private void initview() {
        but_makeanappointment = findViewById(R.id.but_Makeanappointment);
        ed_Makeanappointment = findViewById(R.id.ed_Makeanappointment);
        btn_ok = findViewById(R.id.btn_ok);
        text_data = findViewById(R.id.text_data);
        radioGroup1 = findViewById(R.id.radioGroup1);
    }
}